#!/usr/bin/perl

push @INC, "$ARGV[0]/Contents/Resources";
require "script_vars.pl";
require "SharedScript.pl";

my $INST_TOOL = "$ARGV[0]/Contents/Resources/installer_tool.app/Contents/MacOS/installer_tool";

exit main();

###############################################################################
#
# DeleteOldInstallation()
#
# This function removes previous installations of the product folder from
# the Applications folder.
#
###############################################################################
sub DeleteOldInstallation
{
	my $cmd;
	my $del_appsupport = 1;
	
	print "PREFLIGHT::DeleteOldInstallation>\n";
	# 
	# Delete any old product folders
	#
	if($SUITE_INSTALL == 1 || $PERFECTLAYERS_INSTALL == 1)
	{
		my $product_folder;
		foreach $product_folder (@PERFECTLAYERS_FOLDERS_TO_DELETE)
		{
			doDelete($product_folder, $del_appsupport);
		}
	}

	if($SUITE_INSTALL == 1 || $FOCALPOINT_INSTALL == 1)
	{
		my $product_folder;
		foreach $product_folder (@FOCALPOINT_FOLDERS_TO_DELETE)
		{
			doDelete($product_folder, $del_appsupport);
		}
	}

	if($SUITE_INSTALL == 1 || $GENUINEFRACTALS_INSTALL == 1)
	{
		my $product_folder;
		foreach $product_folder (@PERFECTRESIZE_FOLDERS_TO_DELETE)
		{
			doDelete($product_folder, $del_appsupport);
		}
	}

	if($SUITE_INSTALL == 1 || $PHOTOFRAME_INSTALL == 1)
	{
		my $product_folder;
		foreach $product_folder (@PHOTOFRAME_FOLDERS_TO_DELETE)
		{
			doDelete($product_folder, $del_appsupport);
		}
	}

	if($SUITE_INSTALL == 1 || $PERFECTEFFECTS_INSTALL == 1)
	{
		my $product_folder;

		if($FREE_INSTALL == 1)
		{
			foreach $product_folder (@PERFECTEFFECTSFREE_FOLDERS_TO_DELETE)
			{
				doDelete($product_folder, $del_appsupport);
			}
		}
		else
		{
			if (-d "/Applications/Perfect Layers" || -f "/Applications/Perfect Layers")
			{
				#don't remove effects free appsupport if layers installed
				#layers now includes effects free so it needs the effects free appsupport folder
				$del_appsupport = 0;				
			}
			foreach $product_folder (@PERFECTEFFECTSFREE_FOLDERS_TO_DELETE)
			{
				doDelete($product_folder, $del_appsupport);
			}

			$del_appsupport = 1;
			foreach $product_folder (@PERFECTEFFECTS_FOLDERS_TO_DELETE)
			{
				doDelete($product_folder, $del_appsupport);
			}
		}
	}

	if($SUITE_INSTALL == 1 || $PERFECTMASK_INSTALL == 1)
	{
		my $product_folder;
		foreach $product_folder (@PERFECTMASK_FOLDERS_TO_DELETE)
		{
			doDelete($product_folder, $del_appsupport);
		}
	}

	if($SUITE_INSTALL == 1 || $PERFECTPORTRAIT_INSTALL == 1)
	{
		my $product_folder;
		foreach $product_folder (@PERFECTPORTRAIT_FOLDERS_TO_DELETE)
		{
			doDelete($product_folder, $del_appsupport);
		}
	}
}

###############################################################################
#
# doDelete()
#
###############################################################################
sub doDelete #($product_folder, $del_appsupport)
{
	print "PREFLIGHT::doDelete>\n";
	my $del_appsupport = pop(@_);
	my $product_folder = pop(@_);

	if(length($product_folder) > 0)
	{
		# first look in /Applications
		if (-d "/Applications/$product_folder" || -f "/Applications/$product_folder")
		{
			print "PREFLIGHT::DeleteOldInstallation> Removing $product_folder folder from Applications...\n";
			$cmd = "rm -rf \"/Applications/$product_folder\"";
			print "PREFLIGHT::DeleteOldInstallation> $cmd\n";
			print `$cmd`;
		}
	
		# next look for Aperture plugins
		if (-d "/Library/Application Support/Aperture/Plug-Ins/$product_folder" || -f "/Library/Application Support/Aperture/Plug-Ins/$product_folder")
		{
			print "PREFLIGHT::DeleteOldInstallation> Removing $product_folder folder from Aperture plug-ins...\n";
			$cmd = "rm -rf \"/Library/Application Support/Aperture/Plug-Ins/$product_folder\"";
			print "PREFLIGHT::DeleteOldInstallation> $cmd\n";
			print `$cmd`;
		}
	
		# then look for Lightroom plugins
		if (-d "/Library/Application Support/Adobe/Lightroom/Modules/$product_folder" || -f "/Library/Application Support/Adobe/Lightroom/Modules/$product_folder")
		{
			print "PREFLIGHT::DeleteOldInstallation> Removing $product_folder folder from Lightroom plug-ins...\n";
			$cmd = "rm -rf \"/Library/Application Support/Adobe/Lightroom/Modules/$product_folder\"";
			print "PREFLIGHT::DeleteOldInstallation> $cmd\n";
			print `$cmd`;
		}
	
		if($del_appsupport == 1)
		{
			if($UPDATE_INSTALL == 1)
			{
				#delete old content from app support folder - only remove folders that are included in the updater
				if (-d "/Library/Application Support/onOne Software/$product_folder/Help")
				{
					print "PREFLIGHT::DeleteOldInstallation> Removing $product_folder/Help folder from application support...\n";
					$cmd = "rm -rf \"/Library/Application Support/onOne Software/$product_folder/Help\"";
					print "PREFLIGHT::DeleteOldInstallation> $cmd\n";
					print `$cmd`;
				}
				if (-d "/Library/Application Support/onOne Software/$product_folder/Movies")
				{
					print "PREFLIGHT::DeleteOldInstallation> Removing $product_folder/Movies folder from application support...\n";
					$cmd = "rm -rf \"/Library/Application Support/onOne Software/$product_folder/Movies\"";
					print "PREFLIGHT::DeleteOldInstallation> $cmd\n";
					print `$cmd`;
				}
				
				#photoframe is different
				if (-d "/Library/Application Support/onOne Software/$product_folder/PhotoFrame Help/Help")
				{
					print "PREFLIGHT::DeleteOldInstallation> Removing $product_folder/PhotoFrame Help/Help folder from application support...\n";
					$cmd = "rm -rf \"/Library/Application Support/onOne Software/$product_folder/PhotoFrame Help/Help\"";
					print "PREFLIGHT::DeleteOldInstallation> $cmd\n";
					print `$cmd`;
				}
				if (-d "/Library/Application Support/onOne Software/$product_folder/PhotoFrame Help/Movies")
				{
					print "PREFLIGHT::DeleteOldInstallation> Removing $product_folder/PhotoFrame Help/Movies folder from application support...\n";
					$cmd = "rm -rf \"/Library/Application Support/onOne Software/$product_folder/PhotoFrame Help/Movies\"";
					print "PREFLIGHT::DeleteOldInstallation> $cmd\n";
					print `$cmd`;
				}
			}
			else
			{
				# delete old app support folder
				if (-d "/Library/Application Support/onOne Software/$product_folder" || -f "/Library/Application Support/onOne Software/$product_folder")
				{
					print "PREFLIGHT::DeleteOldInstallation> Removing $product_folder folder from application support...\n";
					$cmd = "rm -rf \"/Library/Application Support/onOne Software/$product_folder\"";
					print "PREFLIGHT::DeleteOldInstallation> $cmd\n";
					print `$cmd`;
				}
			}
		}
	}
}

###############################################################################
#
# main()
#
###############################################################################
sub main
{
	print "PREFLIGHT::main>\n";

	DeleteOldInstallation();

	#######################################################################
	# See if the Installer is running for this user, if so go ahead and
	# do some interactive things on the system
	# This is to prevent us, for example, from opening a Finder window
	# with root level priveleges on a logged out system
	#######################################################################
	my $my_name = MyUsername();
	$cmd = "ps -U $my_name -w | awk '/Installer.app/ && !/awk/'";
	print "POSTFLIGHT::main> Checking for interactive session: $cmd\n";
	my $installer_process = `$cmd`;
	chomp($installer_process);
	print "POSTFLIGHT::main> Installer Process: $installer_process\n";
	my $allow_dialogs = 0;
	if ($installer_process =~ /Installer.app/)
	{
		$allow_dialogs = 1;
	}

	#######################################################################
	# Deactivate the product
	#######################################################################
	if ( $DEACTIVATE_PRODUCT == 1 && $allow_dialogs == 1 )
	{
		# see if there is a serial number we can use, if not we can't do anything
		my $sn_result = `"$INST_TOOL" "checkForActivation" "$PRODUCT_ACTIVATION_CODE" "$ARGV[0]/Contents/Resources/dylib"`;
		chomp($sn_result);
		if ($sn_result =~ /yes/)
		{
			my $dialog_result = `"$INST_TOOL" "alert" "This product will be deactivated in order for the installation to proceed. You will be prompted to reactivate once installation is complete."`;

			# first try to deactivate using the 6.1.1 dylib since that uses a different license table from the latest
			my $reg_result = `"$INST_TOOL" "deactivateProduct" "$PRODUCT_ACTIVATION_CODE" "$ARGV[0]/Contents/Resources/dylib611"`;
			chomp($reg_result);
			if ($reg_result =~ /yes/)
			{
				print "PREFLIGHT::main> 6.1.1 Deactivation succeeded\n";
			}
			else
			{
				print "PREFLIGHT::main> 6.1.1 Deactivation did not succeed\n";
			}
	
			# next, use the latest dylib
			my $reg_result = `"$INST_TOOL" "deactivateProduct" "$PRODUCT_ACTIVATION_CODE" "$ARGV[0]/Contents/Resources/dylib"`;
			chomp($reg_result);
			if ($reg_result =~ /yes/)
			{
				print "PREFLIGHT::main> Deactivation succeeded with latest dylib\n";
			}
			else
			{
				print "PREFLIGHT::main> Deactivation did not succeed with latest dylib\n";
			}
		}
	}

	print "PREFLIGHT::main> success.\n";   
	exit(0);
}
